package cn.raths.controller;


import cn.raths.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "getUserByIdFallbackMethod") // 降级方法
    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Long id){

        String url = "http://userServer/getById/" + id;
        return restTemplate.getForObject(url,User.class);
    }

    public User getUserByIdFallbackMethod(@PathVariable Long id){
        User user = new User();
        user.setId(-1L);
        user.setAddress("被降级了");
        return user;
    }

    //托底方法
    public User getUserByIdFallbackMethod(){
        User user = new User();
        user.setId(-1L);
        user.setName("无此用户");
        user.setAddress("用户服务不可用");
        //返回友好的提示信息
        return user;
    }


}
